#! /bin/make -f
# @file
# @brief Makefile规则 (powerpc-eabivle-gcc)
# @author
# + 隐星魂 (Roy.Sun) <www.starsoul.tech>
# @copyright
# + (c) 2015 隐星魂 (Roy.Sun) <www.starsoul.tech>
# > Licensed under the Apache License, Version 2.0 (the "License");
# > you may not use this file except in compliance with the License.
# > You may obtain a copy of the License at
# >
# >         http://www.apache.org/licenses/LICENSE-2.0
# >
# > Unless required by applicable law or agreed to in writing, software
# > distributed under the License is distributed on an "AS IS" BASIS,
# > WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# > See the License for the specific language governing permissions and
# > limitations under the License.
#

CROSS_COMPILE ?= powerpc-eabivle-
CC := $(CROSS_COMPILE)gcc
AS := $(CROSS_COMPILE)gcc -x assembler-with-cpp
CP := $(CROSS_COMPILE)objcopy
OD := $(CROSS_COMPILE)objdump
AR := $(CROSS_COMPILE)ar
LD := $(CROSS_COMPILE)gcc
SIZE := $(CROSS_COMPILE)size
LUA2HEX := python3 xwbs/util/py/lua2hex.py
RM := -rm

ifeq ($(ARCHCFG_LITTLE_ENDIAN),y)
    ENDIAN := little
else
    ENDIAN := big
endif
~D ?= 1
WARN := -Wall -Wextra -Wundef -Wsign-conversion -Werror
INCDIRS = -I. \
          -I$(XWOS_ARCH_DIR) \
          -I$(XWOS_CPU_DIR) \
          -I$(XWOS_SOC_DIR) \
          -I$(XWOS_BRD_DIR)

PERDEF =

AFLAGS = -mvle -mcpu=e200z0 -m$(ENDIAN) -mregnames -nostdinc -nostdlib \
         -ffunction-sections -fdata-sections \
         -g3 \
         -Wa,-adhlns="$@.lst" $(WARN) $(PERDEF)

CFLAGS = -mvle -mcpu=e200z0 -m$(ENDIAN) -mregnames -std=gnu11 \
         -ffunction-sections -fdata-sections \
         -g3 -fverbose-asm \
         -Wa,-adhlns="$@.lst" $(WARN) $(PERDEF)

MMFLAGS = -MM -MP

ifeq ($(~D),0)
    CFLAGS += -O2
else
    CFLAGS += -O0
endif

ifeq ($(ARCHCFG_COMPILER_CLIB),n)
    CFLAGS += -fno-builtin
endif

LDFLAGS = -mcpu=e200z0 -mhard-float \
          -L$(XWOS_SOC_DIR) -T$(XWOS_BRD_DIR)/$(XuanWuOS_CFG_LDSCRIPT) \
          -Wl,--gc-sections \
          -Wl,-Map=$(XWOS_BRD_DIR)/wkspc/$(TARGET).map,--cref

OBJ_DIR := $(XWOS_WKSPC_DIR)/obj/

include $(XWOS_ARCH_DIR)/arch.mk
include $(XWOS_CPU_DIR)/cpu.mk
include $(XWOS_SOC_DIR)/soc.mk
include $(XWOS_BDL_DIR)/bdl.mk
include $(XWOS_KN_DIR)/xwos.mk
include $(XWBS_UTIL_MK_XWMO)

# colorfully build
ifneq ($(~V),1)
    SHOW_CPP = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[cc -E] "$<" -> "$@" \e[0m";
    SHOW_CC = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[cc] "$<" -> "$@" \e[0m";
    SHOW_CXX = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[c++] "$<" -> "$@" \e[0m";
    SHOW_APP = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[as -E] "$<" -> "$@" \e[0m";
    SHOW_AS = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[as] "$<" -> "$@" \e[0m";
    SHOW_OD = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[od] "$<" -> "$@" \e[0m";
    SHOW_CP = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[cp] "$<" -> "$@" \e[0m";
    SHOW_MM = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[mm] "$<" -> "$@" \e[0m";
    SHOW_AR = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[ar] "$@" \e[0m";
    SHOW_LD = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[ld] "$@" \e[0m";
    SHOW_SIZE = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[size] "$@" \e[0m";
    SHOW_LUA2HEX = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[lua2hex] "$@" \e[0m";
else
    SHOW_CPP =
    SHOW_CC =
    SHOW_CXX =
    SHOW_APP =
    SHOW_AS =
    SHOW_OD =
    SHOW_CP =
    SHOW_MM =
    SHOW_AR =
    SHOW_LD =
    SHOW_SIZE =
    SHOW_LUA2HEX =
endif
